Skip to content

fix(flask): stop reading deprecated flask.__version__#4422

Merged
xrmx merged 9 commits intoopen-telemetry:mainfrom
alliasgher:fix-flask-deprecated-version-attr
Apr 22, 2026
Merged

fix(flask): stop reading deprecated flask.__version__#4422
xrmx merged 9 commits intoopen-telemetry:mainfrom
alliasgher:fix-flask-deprecated-version-attr

Conversation

@alliasgher
Copy link
Copy Markdown
Contributor

Description

Flask 3.1 deprecated the module-level flask.__version__ attribute and emits a DeprecationWarning on access; 3.2 will remove it. The Flask instrumentor was hitting that warning at import time via:

_IS_FLASK_31_PLUS = hasattr(flask, "__version__") and package_version.parse(
    flask.__version__
) >= package_version.parse("3.1.0")

even though it already computes the installed Flask version via importlib.metadata.version("flask") a few lines below. Use that flask_version for the 3.1+ check and drop the deprecated attribute access entirely.

Fixes #4402

Test plan

  • Existing unit tests exercise both Flask 2.x and 3.x+ paths.
  • No behavioural change in _IS_FLASK_31_PLUS — just a different, non-deprecated source for the version string.

Signed-off-by: Ali alliasgher123@gmail.com

@tammy-baylis-swi
Copy link
Copy Markdown
Contributor

Thank you for this @alliasgher . Please could you run tox -e precommit and commit the formatting fixes.

@tammy-baylis-swi tammy-baylis-swi moved this to Reviewed PRs that need fixes in Python PR digest Apr 14, 2026
@alliasgher
Copy link
Copy Markdown
Contributor Author

Done, ran tox -e precommit and committed the ruff formatting fix.

Flask 3.1 deprecated the module-level __version__ attribute and emits a
DeprecationWarning on access; 3.2 will remove it. The Flask
instrumentor was hitting that warning at import time via

    _IS_FLASK_31_PLUS = hasattr(flask, "__version__") and
        package_version.parse(flask.__version__) >= package_version.parse("3.1.0")

even though it already computes the installed Flask version via
importlib.metadata.version("flask") a few lines below. Use that
flask_version for the 3.1+ check and drop the deprecated attribute
access entirely.

Fixes open-telemetry#4402

Signed-off-by: Ali <alliasgher123@gmail.com>
Signed-off-by: alliasgher <alliasgher123@gmail.com>
@alliasgher alliasgher force-pushed the fix-flask-deprecated-version-attr branch from dcf235e to 6d58dbb Compare April 14, 2026 23:29
@xrmx
Copy link
Copy Markdown
Contributor

xrmx commented Apr 15, 2026

I think you missed the other user:

instrumentation/opentelemetry-instrumentation-flask/tests/test_flask_compatibility.py:        self.flask_version = flask.__version__

Per @xrmx review — the test suite was the remaining caller of the
deprecated attribute. Switch to importlib.metadata.version("flask")
to match the production code change.

Signed-off-by: alliasgher <alliasgher123@gmail.com>
@alliasgher
Copy link
Copy Markdown
Contributor Author

alliasgher commented Apr 15, 2026

Good catch, pushed 93fb7da switching the test to importlib.metadata.version("flask") as well. @xrmx

@github-project-automation github-project-automation Bot moved this from Reviewed PRs that need fixes to Approved PRs in Python PR digest Apr 15, 2026
Comment thread CHANGELOG.md Outdated
xrmx added 2 commits April 21, 2026 18:31
Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
@xrmx xrmx enabled auto-merge (squash) April 21, 2026 16:31
@xrmx xrmx disabled auto-merge April 22, 2026 13:14
@xrmx xrmx enabled auto-merge (squash) April 22, 2026 13:15
@xrmx xrmx merged commit 22de1c1 into open-telemetry:main Apr 22, 2026
751 checks passed
@github-project-automation github-project-automation Bot moved this from Approved PRs to Done in Python PR digest Apr 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

flask.__version__ is deprecated

4 participants